
import { makeAutoObservable } from "mobx";
import React from "react";
import * as apis from "@shared/apis";

export class PageController {

    static Context = React.createContext<PageController>(undefined as any);

    /** 客户ID */
    id = '';

    /** 客户详情 */
    data?: apis.customers.ICustomer;

    /** 加载状态 */
    loading = 0;

    /** 客户所有储值卡 */
    customerStoredValueCards?: apis.customers.getStoredValueCards.IGetStoredValueCardsResponse;

    constructor() {
        makeAutoObservable(this);
    }

    /** 设置客户ID */
    setId = (id: string) => {
        this.id = id;
    }

    /** 查询指定客户详情 */
    queryCustomerDetail = async () => {
        this.loading++;
        try {
            if (this.id) {
                this.data = await apis.customers.detail.get(this.id);
            } else {
                this.data = undefined
            }
        } catch (error) {
            this.data = undefined;
            console.log(error);
        }
        this.loading--;
    }

    /** 查询指定客户的所有会员卡 */
    queryCustomerStoredValueCards = async () => {
        this.loading++;
        try {
            if (this.id) {
                const cards = await apis.customers.getStoredValueCards.get(this.id);
                this.customerStoredValueCards = cards;
            } else {
                this.customerStoredValueCards = undefined;
            }
        } catch (error) {
            this.customerStoredValueCards = undefined;
            console.log(error);
        }
        this.loading--;
    }
}


